package com.hjs.servlet.regularinspection.pavementdamage;

import com.hjs.bean.PavementDamage;
import com.hjs.bean.PavementDamageCom;
import com.hjs.bean.RegularInspection;
import com.hjs.mapper.RegularInspectionMapper;
import com.hjs.tools.EvaluationUtil;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PavementDamage/add")
@Slf4j
public class AddPavementDamage extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RegularInspectionMapper regularInspectionMapper = sqlSession.getMapper(RegularInspectionMapper.class);
            RegularInspection regularInspection = regularInspectionMapper.getOneRegularInspectionById(requestUtil.getInt("regularInspectionID"));
            Integer result = addPavementDamage(requestUtil,sqlSession);
            boolean sqlResult = false;
            if (result != -1) {
                sqlResult= regularInspectionMapper.updatePavementDamageComIDRegularInspectionById(regularInspection.getRegularInspectionID(),result);
                if(sqlResult && regularInspection.getFlatnessID() != null) {
                    sqlResult = EvaluationUtil.evaluationResult(requestUtil,sqlSession,2);
                }
            }
            Mapper.SessionEnd(sqlResult, sqlSession,"添加路面损坏情况失败");
            requestUtil.responseIsSuccess(sqlResult,"添加失败");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("添加路面损坏情况失败");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request,response);
    }

    private boolean addAllPavementDamage(RequestUtil requestUtil, RegularInspectionMapper mapper, PavementDamageCom pavementDamageCom) {
        String[] properties = {"damageTypeID", "damageLength", "damageWidth","damageHeight","damagePAC","remarks"};
        /*
         * 提供参数
         * comNumber
         * damageTypeID
         * damageLength
         * damageWidth
         * damageHeight
         * damagePAC
         * remarks （可选）
         */
        for (int i = 0; i < requestUtil.getInt("comNumber"); i++) {
            boolean sqlResult = mapper.addOnePavementDamage(new PavementDamage(
                    null, pavementDamageCom, requestUtil.getInt(properties[0] + i),
                    requestUtil.getDouble(properties[1] + i), requestUtil.getDouble(properties[2] + i),
                    requestUtil.getDouble(properties[3] + i),requestUtil.getString(properties[4] + i),
                    requestUtil.getString(properties[5] + i)));
            if (!sqlResult){
                return false;
            }
        }
        return true;
    }

    private Integer addPavementDamage(RequestUtil requestUtil, SqlSession sqlSession) {
        try {
            /*
             * 提供参数
             * inspectionDate
             * inspectorID
             * SAEPlace
             * totalLength
             * totalWidth
             * regularInspectionID
             */
            PavementDamageCom pavementDamageCom = new PavementDamageCom(null,
                    requestUtil.getDate("inspectionDate"),requestUtil.getString("inspectorID"),
                    requestUtil.getString("SAEPlace"),requestUtil.getDouble("totalLength"),
                    requestUtil.getDouble("totalWidth"),
                    requestUtil.getDouble("totalLength")*requestUtil.getDouble("totalWidth"),
                    requestUtil.getInt("regularInspectionID"));
            RegularInspectionMapper mapper = sqlSession.getMapper(RegularInspectionMapper.class);
            boolean sqlResult = mapper.addOnePavementDamageCom(pavementDamageCom);
            if (sqlResult) {
                sqlResult = addAllPavementDamage(requestUtil, mapper, pavementDamageCom);
            }
            Mapper.SessionEnd(sqlResult, sqlSession,"添加路面损坏情况失败");
            requestUtil.responseIsSuccess(sqlResult,"添加失败");
            return pavementDamageCom.getPavementDamageComID();
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("添加路面损坏情况失败catch");
            return -1;
        }
    }
}
